import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileCopyExample {
    public static void main(String[] args) {
        String sourceFile = "D:/Users/Desktop/source.txt/";
        String byteCopyFile = "byte_copy.txt";
        String charCopyFile = "char_copy.txt";

        try {
            copyFileUsingByteStream(sourceFile, byteCopyFile);
            copyFileUsingCharacterStream(sourceFile, charCopyFile);
            System.out.println("文件复制成功！");
        } catch (IOException e) {
            System.out.println("文件复制时出错: " + e.getMessage());
        }
    }

    public static void copyFileUsingByteStream(String source, String destination) throws IOException {
        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(destination)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        }
    }

    public static void copyFileUsingCharacterStream(String source, String destination) throws IOException {
        try (FileReader fr = new FileReader(source);
             FileWriter fw = new FileWriter(destination)) {
            char[] buffer = new char[1024];
            int charsRead;
            while ((charsRead = fr.read(buffer)) != -1) {
                fw.write(buffer, 0, charsRead);
            }
        }
    }
}    